// @Date         2021-11-18
// @Author       Aaron Zhang
// @Description 初始化Provider
// @FileName      provider.go

package command

import (
	"errors"
	"fmt"
	"github.com/scylladb/termtables"
	"log"
	"zin/framework/cobra"
	"zin/framework/command/template"
	"zin/framework/util"
)

func initProviderCommand() *cobra.Command {
	parseProviderFlags()
	providerCommand.AddCommand(providerListCommand, providerNewCommand, providerCreateCommand)
	return providerCommand
}

var providerCommand = &cobra.Command{
	Use:   "provider",
	Short: "服务提供者相关操作",
	RunE: func(cmd *cobra.Command, args []string) error {
		// 打印帮助文档
		cmd.Help()
		return nil
	},
}

var providerListCommand = &cobra.Command{
	Use:   "list",
	Short: "打印所有服務提供者",
	RunE: func(cmd *cobra.Command, args []string) error {
		providers := cmd.GetContainer().ListProviders()
		t := termtables.CreateTable()
		t.AddHeaders("name")
		for _, v := range  providers["bind"] {
			t.AddRow(v)
		}
		res := t.Render()
		fmt.Println(res)
		return nil
	},
}

var providerNewCommand = &cobra.Command{
	Use:   "new",
	Short: "初始化一个服务提供者",
	Long:  "与create的区别是， create创建文件就用字符串替换的形式， new就通过内置的template创建",
	RunE: func(cmd *cobra.Command, args []string) error {
		//contract
		mf := map[string]interface{}{
			"name":  name,
			"desc":  description,
			"date":  util.ConvertNowDate2String(util.DateFormat2),
			"defer": isDefer,
		}
		var err error

		if name == ""{
			return errors.New("请制定服务提供者名称，包括：--name test")
		}

		//create contract
		err = template.CreateContractByTemplate(name, mf)

		// create provider
		if err == nil {
			err = template.CreateProviderByTemplate(name, mf)
		}
		// create provider
		if err == nil {
			err = template.CreateProviderServiceByTemplate(name, mf)
		}

		if err == nil {
			for _, v := range template.FileArr {
				log.Printf("%s create success\n", v)
			}
		}
		return err
	},
}
var providerCreateCommand = &cobra.Command{
	Use:   "create",
	Short: "初始化一个服务提供者",
	Long:  "与new的区别是， create创建文件就用字符串替换的形式， new就通过内置的template创建",
	RunE: func(cmd *cobra.Command, args []string) error {
		var err error

		if name == ""{
			return errors.New("请制定服务提供者名称，包括：--name test")
		}

		//create contract
		err = template.CreateProviderContract(name, description)

		// create provider
		if err == nil {
			err = template.CreateProviderProvider(name, description, isDefer)
		}

		//create service
		if err == nil {
			err = template.CreateProviderService(name, description)
		}

		if err != nil {
			return err
		}
		for _, v := range template.FileArr {
			log.Printf("%s create success\n", v)
		}
		return nil
	},
}

//解析 create,new 后面的 flag参数，控制是否后台运行
var name string        // providerName
var isDefer bool       // is defer load
var description string // file description

func parseProviderFlags() {
	providerCreateCommand.Flags().StringVarP(&name, "name", "n", "", "服务提供者名称")
	providerCreateCommand.Flags().StringVarP(&description, "desc", "D", "", "文件功能描述")
	providerCreateCommand.Flags().BoolVarP(&isDefer, "defer", "d", false, "是否延迟加载")

	providerNewCommand.Flags().StringVarP(&name, "name", "n", "", "服务提供者名称")
	providerNewCommand.Flags().StringVarP(&description, "desc", "D", "", "文件功能描述")
	providerNewCommand.Flags().BoolVarP(&isDefer, "defer", "d", false, "是否延迟加载")
}
